package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.entity.*;
import com.workcell.workdata.xthings.mapper.CodeRotaryKilnMapper;
import com.workcell.workdata.xthings.service.BaseNaturalGasService;
import com.workcell.workdata.xthings.service.BaseRotaryKilnService;
import com.workcell.workdata.xthings.service.CodeRotaryKilnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 码库回转窑信息表
 *
 * @author pigx code generator
 * @date 2023-07-24 10:26:18
 */
@Service
public class CodeRotaryKilnServiceImpl extends ServiceImpl<CodeRotaryKilnMapper, CodeRotaryKiln> implements CodeRotaryKilnService {

    @Resource
    private BaseRotaryKilnService baseRotaryKilnService;

    /**
     * 获取硬件清单
     * @return
     */
    public List<JSONObject> getHardwareList() {
        LambdaQueryWrapper<BaseRotaryKiln> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseRotaryKiln.class);
        lambdaQueryWrapper.eq(BaseRotaryKiln::getState, HardwareStateEnum.INUSE.getCode());
        return baseRotaryKilnService.list(lambdaQueryWrapper).stream().map(i -> {
            JSONObject resultBean = new JSONObject();
            resultBean.put("baseId",i.getId());
            resultBean.put("manufacturer",i.getManufacturer());
            resultBean.put("hardwareName",i.getName());
            return resultBean;
        }).collect(Collectors.toList());
    }

    /**
     * 查询硬件快照信息
     * @param codeId
     * @return
     */
    public Object getHardwareInfo(String codeId) {
        return this.getById(codeId);
    }

    /**
     * 查询硬件基本信息
     * @param baseIds
     * @return
     */
    public Object getHardwareBase(String baseIds) {
        List<String> baseList = Arrays.asList(baseIds.split(","));
        LambdaQueryWrapper<BaseRotaryKiln> baseRotaryKilnLambdaQueryWrapper = new QueryWrapper<BaseRotaryKiln>().lambda()
                .in(CollectionUtil.isNotEmpty(baseList),BaseRotaryKiln::getId,baseList);
        List<BaseRotaryKiln> baseRotaryKilnList = baseRotaryKilnService.list(baseRotaryKilnLambdaQueryWrapper);
        return baseRotaryKilnList;
    }

    /**
     * 保存二维码基本快照信息
     * @param codeChainnum
     * @return
     */
    public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum) {
        // 查询硬件信息
        LambdaQueryWrapper<BaseRotaryKiln> baseCondition = new QueryWrapper<BaseRotaryKiln>().lambda()
                .eq(BaseRotaryKiln::getId,codeChainnum.getBaseId());
        BaseRotaryKiln baseRotaryKiln = baseRotaryKilnService.getOne(baseCondition);
        if (ObjectUtil.isEmpty(baseRotaryKiln)){
            return false;
        }
        // 添加二维码快照信息
        CodeRotaryKiln saveBean = new CodeRotaryKiln();
        BeanUtil.copyProperties(baseRotaryKiln,saveBean, "createBy","createTime","updateBy","updateTime");
        saveBean.setCodeId(codeChainnum.getCodeId());
        saveBean.setChainNum(codeChainnum.getChainNum());
        saveBean.setDeviceNo(codeChainnum.getDeviceNo());
        return this.save(saveBean);
    }
}
